<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/action/AbstractListAction.php");
require_once("cms/image/om/TinyImagePeer.php");
require_once("cms/core/om/BinaryFilePeer.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting images");

        $c = new Criteria();
        $c->add(TinyImagePeer::TINY_IMAGE_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            $images = TinyImagePeer::doSelect($c, $conn);
            $c = new Criteria();
            foreach($images as $image) {

                $c->add(BinaryFilePeer::BINARY_FILE_ID,
                    $image->getImageRelatedByThumbnailId()->getBinaryFileId());
                BinaryFilePeer::doDelete($c, $conn);

                $c->add(BinaryFilePeer::BINARY_FILE_ID,
                    $image->getImageRelatedByImageId()->getBinaryFileId());
                BinaryFilePeer::doDelete($c, $conn);
            }

            $conn->commit();

            // Set user success message
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));

        } catch(PropelException $e) {
            $conn->rollback();
            $this->log->error($e->getMessage());
            // Set user error message
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
        }

    }

}
?>